package com.five.monkey.feign.consumer.controller;

import com.five.monkey.constant.CommonConstant;
import com.five.monkey.feign.consumer.client.UserFeignClient;
import com.five.monkey.vo.CommonVo;
import com.five.monkey.vo.PageVo;
import com.five.monkey.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * @author 狻猊
 * @date 2021/8/10 下午5:49
 */
@RestController
@RequestMapping("/consumer/user")
public class ConsumerUserController {

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/login")
    public CommonVo<UserVo> login(@RequestParam("name") String name, @RequestParam("password") String password, HttpServletResponse response) {
        CommonVo<UserVo> vo = userFeignClient.login(name, password);
        if (vo.getCode() == CommonConstant.SUCCESS_CODE) {
            UserVo userVo = vo.getData();
            if (Objects.nonNull(userVo)) {
                String uuid = UUID.randomUUID().toString() + CommonConstant.UNDERSCORE + userVo.getId();
                Cookie cookie = new Cookie(CommonConstant.COOKIE_NAME, uuid);
                response.addCookie(cookie);
            } else {
                vo.setCode(CommonConstant.UNAUTHORIZED_CODE).setDesc("登录失败，用户名或密码错误");
            }
        }
        return vo;
    }

    @GetMapping("/find/by/{id}")
    public CommonVo<UserVo> findById(@PathVariable("id") Long id) {
        return userFeignClient.findById(id);
    }

    @PostMapping("/save/to/elasticsearch")
    public CommonVo<UserVo> saveToElasticsearch(@RequestBody UserVo userVo) {
        return userFeignClient.saveToElasticsearch(userVo);
    }

    @GetMapping("/find/by/{id}/elasticsearch")
    public CommonVo<UserVo> findByIdFromElasticsearch(@PathVariable("id") Long id) {
        return userFeignClient.findByIdFromElasticsearch(id);
    }

    @DeleteMapping("/delete/by/{id}/elasticsearch")
    public CommonVo<Boolean> deleteByIdFromElasticsearch(@PathVariable("id") Long id) {
        return userFeignClient.deleteByIdFromElasticsearch(id);
    }

    @GetMapping("/find/all/elasticsearch")
    public CommonVo<List<UserVo>> findAllFromElasticsearch() {
        return userFeignClient.findAllFromElasticsearch();
    }

    @GetMapping("/page/by/{status}/elasticsearch")
    public CommonVo<PageVo<UserVo>> pageByStatusFromElasticsearch(@PathVariable("status") Integer status,
                                                                  @RequestParam(value = "page") int page,
                                                                  @RequestParam(value = "size", required = false, defaultValue = "10") int size) {
        return userFeignClient.pageByStatusFromElasticsearch(status, page, size);
    }

    @GetMapping("/list/by/{status}/elasticsearch")
    public CommonVo<List<UserVo>> listByStatusFromElasticsearch(@PathVariable("status") Integer status) {
        return userFeignClient.listByStatusFromElasticsearch(status);
    }
}
